<?php
namespace app\sysAdmin\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Emodel;
use think\Db;

class TeacherGroupManage extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		if(request()->isPost()){
			$page = input('page');
			$rows = input('rows');
			$result = Db::table("T_ORGANIZE_GROUP")
			->page($page, $rows)->order("PARENT_ID,WEIGHT")->select();
			$count = Db::table("T_ORGANIZE_GROUP")->count();
			$data = selectResult($result);
			$list = [];
	        foreach ($data as $key => $obj) {
	        	$groupCode = $obj["GROUP_CODE"];
	        	if("SUBJECT_GROUP" == $groupCode) {
	        		$obj["positionText"]  = getSubjectName($obj["IDS"]);
	        	}else if("GRADE_GROUP" == $groupCode) {
	        		$obj["positionText"]  = getGradeNameByGradeId($obj["IDS"]);
	        	}else {
	        		$obj["positionText"]  = getPositionName($obj["IDS"]);
	        	}
	            $list[$key] = $obj;
	        }
			$grid['rows'] = $list;
			$grid['total'] = $count;
			return $grid;
		}
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("ID", $all)) {
				update('T_ORGANIZE_GROUP', $all);
			} else {
				insert('T_ORGANIZE_GROUP', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function parentMenu() {
		$where["PARENT_ID"] = 0; 
		$result = Db::table("T_ORGANIZE_GROUP")
				->field("ID,GROUP_NAME,WEIGHT")
				->where($where)
				->order("WEIGHT")
				->select();
		return selectResult($result);
	}

	public function organizeGroup() {
		$groupCode = input("groupCode");
		$sql = "";
		if('GRADE_GROUP' == $groupCode) {
			$sql="select ID, '年级' DEPART, 20000 PARENT_ID, 年级名称 POSITION from 年级名称";
		}else if ('SUBJECT_GROUP' == $groupCode) {
			$sql="select ID, '学科' DEPART, 30000 parent_ID, 学科名称 POSITION from 学科名称";
		}else {
			$sql="select od.ID, pt.DEPART, od.PARENT_ID, od.POSITION 
		  from T_ORGANIZE_DEPART od
		  inner join (
			select ID, DEPART from T_ORGANIZE_DEPART where PARENT_ID = 0
		  ) pt on od.PARENT_ID = pt.ID
		  where od.PARENT_ID > 0
		  order by WEIGHT";
		}
		return Db::query($sql);
	}

}
